
using LF.Application.Services.interfaces;
using LF.Domain.Entities;
using LF.Domain.Services;
using LF.Application.Services.Iml;
using LF.Shared.Dtos.Out;
using LF.Shared.Dtos.Input;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp.ObjectMapping;
using LF.Common;
using Microsoft.AspNetCore.Mvc;

namespace LF.Application.Services
{
    [Authorize]
    [ApiExplorerSettings(GroupName = SwaggerGroup.Sys)]
    public class ChinaService: BaseService, IChinaService
    {
        IChinaManagerService _manager;
        public ChinaService(IChinaManagerService manager, IObjectMapper<LFModule> mapper,
            IGlobalManagerService globalManager) : base(mapper, globalManager)
        {
            _manager = manager;
        }

        public Result<ChinaSearchOut> Chinas(ChinaSearch search)
        {
            var result = _manager.All(search.Index, search.PageSize, search.Key);
            var list = _mapper.Map<List<China>, List<ChinaOut>>(result);
            var count = _manager.Count(search.Key);
            return Ok(new ChinaSearchOut { Items = list, TotalCount = count });
        }

        public Result<ChinaOut> Get(int id)
        {
            var item = _manager.Get(id);
            var result = _mapper.Map<China, ChinaOut>(item);
            return Ok(result);
        }

        public Result<dynamic> GetProvinceAndCity(int id)
        {
            var item=_manager.Get(id);
            var province = _manager.GetProvinceByCode(item.Province);
            var city = _manager.GetCityByCode(item.Province,item.City);
            var district = _manager.GetDistrictByCode(item.Province, item.City, item.Area);
            var s = _manager.GetAddressInfo(id);
            return new Result<dynamic> { Data= new { province = province?.Name, city = (city==null? province?.Name : city?.Name),district= district?.Name } };
        }
        public Result<List<ChinaOut>> PostAddress(ChinaInput input)
        {
            List<China> list;
            switch (input.AddrType)
            {
                case AddrType.Province:
                default:
                    list = _manager.Province();
                    break;
                case AddrType.City:
                    list = _manager.City(input.Province);
                    break;
                case AddrType.Area:
                    list = _manager.Area(input.Province,input.City);
                    break;
                case AddrType.Town:
                    list = _manager.Town(input.Province, input.City,input.Area);
                    break;
            }
            var result = _mapper.Map<List<China>, List<ChinaOut>>(list);
            return Ok(result);
        }
    }
}
